package ru.inse.core.common;

/**
 * @author evga
 *
 */
public class TypeConvert
{
    static public String toString(Object value, String defaultValue)
    {
        if (value == null)
        {
            return defaultValue;
        }
        return value.toString();
    }

    static public Integer toInt(Object value, Integer defaultValue)
    {
        if (value == null)
        {
            return defaultValue;
        }
        if (value instanceof Number)
        {
            return ((Number)value).intValue();
        }
        else
        {
            try
            {
                return Integer.valueOf(value.toString());
            }
            catch (Exception e)
            {
                return defaultValue;
            }
        }
    }

    static public Long toLong(Object value, Long defaultValue)
    {
        if (value == null)
        {
            return defaultValue;
        }
        if (value instanceof Number)
        {
            return ((Number)value).longValue();
        }
        else
        {
            try
            {
                return Long.valueOf(value.toString());
            }
            catch (Exception e)
            {
                return defaultValue;
            }
        }
    }

    static public Boolean toBool(Object value, Boolean defaultValue)
    {
        if (value == null)
        {
            return defaultValue;
        }
        if (value instanceof Boolean)
        {
            return ((Boolean)value);
        }
        else
        {
            try
            {
                return Boolean.valueOf(value.toString());
            }
            catch (Exception e)
            {
                return defaultValue;
            }
        }
    }
}
